package com.kingbook.api.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.kingbook.core.annotations.ContainProperties;
import com.kingbook.core.auth.LoginManager;
import com.kingbook.core.result.JSONResult;
import com.kingbook.services.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.kingbook.core.auth.LoginManager.AUTH;
import static com.kingbook.enums.CategoryEnum.SPEND;

/**
 * @Author Bucky
 * @Date 2020-01-08 21:13:15
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("category")
public class CategoryRest {

    @Autowired
    CategoryService categoryService;
    @Autowired
    CustomerService customerService;
    @Autowired
    PartnerService partnerService;
    @Autowired
    DriverService driverService;
    @Autowired
    VehicleService vehicleService;
    @Autowired
    WorkspaceService workspaceService;
    @Autowired
    TonsService tonsService;
    @Autowired
    WorkorderService workorderService;

    @PutMapping
    @ContainProperties({"mid", "category", "name"})
    public JSONResult save(@RequestBody Map<String, Object> param) {
        return categoryService.save(param);
    }

    @PostMapping
    @ContainProperties({"id"})
    public JSONResult modify(@RequestBody Map<String, Object> param) {
        return categoryService.modify(param);
    }

    @PostMapping("query")
    public JSONResult query(@RequestBody Map<String, Object> param) {
        return categoryService.query(param);
    }

    @PostMapping("pages")
    @ContainProperties({"pageNo", "pageSize"})
    public JSONResult queryForPages(@RequestBody Map<String, Object> param) {
        return categoryService.queryForPages(param);
    }

    @GetMapping("configForWorkspace")
    public JSONResult configForWorkspace(@RequestHeader(name = AUTH) String auth) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("mid", LoginManager.getMid(auth));
        JSONObject json = new JSONObject();
        json.put("customerList", getArrayByProperty(customerService.queryMap(param), "name"));
        json.put("partnerList", getArrayByProperty(partnerService.queryMap(param), "name"));
        return JSONResult.success(json);
    }

    @GetMapping("configForVehicle")
    public JSONResult configForVehicle(@RequestHeader(name = AUTH) String auth) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("mid", LoginManager.getMid(auth));
        JSONObject json = new JSONObject();
        json.put("driverList", getArrayByProperty(driverService.queryMap(param), "name"));
        json.put("tonsList", getArrayByProperty(workorderService.getDistinctTons(param), "tons"));
        json.put("brandList", getArrayByProperty(vehicleService.getDistinctBrands(param), "name"));
        json.put("modelList", getArrayByProperty(vehicleService.getDistinctModels(param), "name"));
        json.put("insuranceCompanyList", getArrayByProperty(vehicleService.getDistinctInsuranceCompany(param), "name"));
        return JSONResult.success(json);
    }

    @GetMapping("configForOrder")
    public JSONResult configForOrder(@RequestHeader(name = AUTH) String auth) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("mid", LoginManager.getMid(auth));
        JSONObject json = new JSONObject();
        json.put("customerInfo", customerService.queryCustomerAndWorkspace(param).getData());
        json.put("partnerInfo", partnerService.queryPartnerAndWorkspace(param).getData());
        json.put("toPartnerInfo", partnerService.queryPartnerAndVehicle(param).getData());
        json.put("vehicleInfo", vehicleService.queryVehicleInfo(param).getData());
        json.put("driverList", getArrayByProperty(driverService.queryMap(param), "name"));
        json.put("tonsList", getArrayByProperty(workorderService.getDistinctTons(param), "tons"));
        return JSONResult.success(json);
    }

    @GetMapping("condition")
    public JSONResult conditionQuery(@RequestHeader(name = AUTH) String auth) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("mid", LoginManager.getMid(auth));
        JSONObject json = new JSONObject();
        json.put("vid", getJSONArrayByProperty(vehicleService.queryMap(param), "id", "name"));
        json.put("cid", getJSONArrayByProperty(workorderService.getDistinctCustomer(param), "cid", "name"));
        json.put("pid", getJSONArrayByProperty(workorderService.getDistinctPartner(param), "pid", "name"));
        json.put("wid", getJSONArrayByProperty(workorderService.getDistinctWorkspace(param), "wid", "address"));
        json.put("tons", getJSONArrayByProperty(workorderService.getDistinctTons(param), "tons", "tons"));
        json.put("startDate", workorderService.getStartDate(param));
        return JSONResult.success(json);
    }

    @GetMapping("conditionAllot")
    public JSONResult conditionAllotQuery(@RequestHeader(name = AUTH) String auth) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("pid", LoginManager.getMid(auth));
        param.put("use_type", 2);
        JSONObject json = new JSONObject();
        json.put("allotPid", getJSONArrayByProperty(workorderService.getDistinctAllotPartner(param), "mid", "name"));
        json.put("wid", getJSONArrayByProperty(workorderService.getDistinctWorkspace(param), "wid", "address"));
        param.put("mid", LoginManager.getMid(auth));
        json.put("tons", getJSONArrayByProperty(workorderService.getDistinctTons(param), "tons", "tons"));
        json.put("startDate", workorderService.getAllotStartDate(param));
        return JSONResult.success(json);
    }


    @GetMapping("configForSpend")
    public JSONResult configForSpend(@RequestHeader(name = AUTH) String auth) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("mid", LoginManager.getMid(auth));
        JSONObject json = new JSONObject();
        json.put("subjectList", getArrayByProperty(categoryService.queryMap(SPEND), "name"));
        json.put("driverList", getArrayByProperty(driverService.queryMap(param), "name"));
        json.put("vehicleList", getArrayByProperty(vehicleService.queryMap(param), "name"));
        return JSONResult.success(json);
    }

    private String[] getArrayByProperty(Map<String, ?>[] maps, String key) {
        if (ArrayUtils.isEmpty(maps)) {
            return new String[0];
        }
        String[] arr = new String[maps.length];
        int i = 0;
        for (Map<String, ?> map : maps) {
            arr[i] = MapUtils.getString(map, key, "");
            i++;
        }
        return arr;
    }

    private JSONArray getJSONArrayByProperty(Map<String, ?>[] maps, String id, String name) {
        if (ArrayUtils.isEmpty(maps)) {
            return new JSONArray();
        }
        JSONArray arr = new JSONArray();
        for (Map<String, ?> map : maps) {
            JSONObject json = new JSONObject();
            json.put("id", MapUtils.getIntValue(map, id, 0));
            json.put("name", MapUtils.getString(map, name, ""));
            json.put("selected", false);
            arr.add(json);
        }
        return arr;
    }

}
